package com.lc.pianostudent.util;

import java.util.regex.Pattern;

/**
 * 校验器：利用正则表达式校验邮箱、手机号等
 *
 * @author liujiduo
 */
public class Validator {
    /**
     * 正则表达式：验证用户名
     */
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";

    /**
     * 正则表达式：验证密码
     */
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{8,12}$";
    public static final String REGEX_PASSWORD1 = "^[a-zA-Z0-9]{6,20}$";
    public static final String REGEX_PASSWORD2 = "^[a-zA-Z0-9]{6,12}$";
    /**
     * 正则表达式：验证手机号
     */
    public static final String REGEX_MOBILE = "^1[3|4|5|7|8]\\d{9}$";

    /**
     * 正则表达式：验证邮箱
     */
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    /**
     * 正则表达式：验证汉字
     */
    public static final String REGEX_CHINESE = "^([\\u4e00-\\u9fa5]{2,12})";

    /**
     * 正则表达式：验证身份证
     */
    public static final String REGEX_ID_CARD = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";

    /**
     * 正则表达式：验证IP地址
     */
    public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";


    //验证邮编
    public static final String REGEX_POST_CODE = "^[1-9][0-9]{5}$";


    public static final String REGEX_NICK_NAME = "[\\u4e00-\\u9fa5_a-zA-Z0-9_]{2,10}";
    //验证座机
    public static final String REGEX_ISZUOJI = "((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$) ";

    //验证车牌号
    public static final String CARNUM_REGEX = "[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}";

    //验证银行卡号
    public static final String REGEX_BANK_NUM = "^([0-9]{16}|[0-9]{19})$";

    public static final String CASH = "^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$";

    /**
     *
     * 验证金额
     * */
    public static boolean isCash(String str) {

        return Pattern.matches(CASH, str);

    }


    /**
     * 验证银行卡号
     *
     * @param str
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isBankNum(String str) {
        return Pattern.matches(REGEX_BANK_NUM, str);
    }

    /**
     * 校验车牌号
     *
     * @param str
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isCarNum(String str) {
        return Pattern.matches(CARNUM_REGEX, str);
    }

    /**
     * 判断是否座机
     *
     * @param string
     * @return
     */
    public static boolean isZuoJi(String string) {
        return Pattern.compile(REGEX_ISZUOJI).matcher(string).matches();
    }

    /**
     * 判断邮编
     *
     * @param zipString
     * @return
     */
    public static boolean isPostCode(String zipString) {
        return Pattern.compile(REGEX_POST_CODE).matcher(zipString).matches();
    }

    /**
     * 校验用户名
     *
     * @param username
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isUsername(String username) {
        return Pattern.matches(REGEX_USERNAME, username);
    }

    /**
     * 校验密码
     *
     * @param password
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isPassword(String password) {
        return Pattern.matches(REGEX_PASSWORD, password);
    }

    public static boolean isPassword1(String password) {
        return Pattern.matches(REGEX_PASSWORD1, password);
    }
    public static boolean isPassword2(String password) {
        return Pattern.matches(REGEX_PASSWORD2, password);
    }
    /**
     * 校验昵称
     *
     * @param nickname
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isNickName(String nickname) {
        return Pattern.matches(REGEX_NICK_NAME, nickname);
    }

    /**
     * 校验手机号
     *
     * @param mobile
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isMobile(String mobile) {
        return Pattern.matches(REGEX_MOBILE, mobile);
    }

    /**
     * 校验邮箱
     *
     * @param email
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isEmail(String email) {
        return Pattern.matches(REGEX_EMAIL, email);
    }

    /**
     * 校验汉字
     *
     * @param chinese
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isChinese(String chinese) {
        return Pattern.matches(REGEX_CHINESE, chinese);
    }

    /**
     * 校验身份证
     *
     * @param idCard
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isIDCard(String idCard) {
        return Pattern.matches(REGEX_ID_CARD, idCard);
    }

//    /**
//     * 校验URL
//     *
//     * @param url
//     * @return 校验通过返回true，否则返回false
//     */
//    public static boolean isUrl(String url) {
//        return Pattern.matches(REGEX_URL, url);
//    }

    /**
     * 校验IP地址
     *
     * @param ipAddr
     * @return
     */
    public static boolean isIPAddr(String ipAddr) {
        return Pattern.matches(REGEX_IP_ADDR, ipAddr);
    }

//    public static void main(String[] args) {
//        String username = "fdsdfsdj";
//        System.out.println(Validator.isUsername(username));
//        System.out.println(Validator.isChinese(username));
//    }
}